package com.shuorikeji.huohuo.view;

import android.graphics.Camera;
import android.graphics.Matrix;
import android.os.Build;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

public class JapaneseComicPageTransformer extends BasePageTransformer{
    private static final float MIN_SCALE = 0.85f;
    private static final float MAX_ROTATION = 30f;
    private final Camera camera = new Camera();
    private final Matrix matrix = new Matrix();

    @RequiresApi(api = Build.VERSION_CODES.Q)
    @Override
    public void transformPage(@NonNull View page, float position) {
        resetTransform(page);
        float absPosition = Math.abs(position);

        // 计算缩放比例
        float scaleFactor = MIN_SCALE + (1 - MIN_SCALE) * (1 - absPosition);

        // 计算旋转角度
        float rotation = -MAX_ROTATION * position;

        // 应用变换
        page.setScaleX(scaleFactor);
        page.setScaleY(scaleFactor);

        // 使用Camera实现3D效果
        camera.save();
        camera.rotateY(rotation);
        camera.getMatrix(matrix);
        camera.restore();

        // 调整矩阵中心点
        float centerX = page.getWidth() / 2f;
        float centerY = page.getHeight() / 2f;
        matrix.preTranslate(-centerX, -centerY);
        matrix.postTranslate(centerX, centerY);

        // 应用矩阵变换
        page.setAnimationMatrix(matrix);

        // 设置透明度
        page.setAlpha(1 - absPosition);

        // 设置层级
        if (position <= 0) {
            page.setTranslationZ(1);
        } else {
            page.setTranslationZ(-1);
        }
    }
}
